package util

import (
	"agent-list---web-development/list/model"
	"agent-list---web-development/list/response"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
)

func GetTask(ctx *gin.Context) (task *model.Task) {
	account := ctx.PostForm("account")
	title := ctx.PostForm("title")
	starttimeStr := ctx.PostForm("starttime")
	endtimeStr := ctx.PostForm("endtime")

	if len(title) == 0 {
		response.Response(ctx, http.StatusInternalServerError, 422, nil, "内容不能为空")
		return
	}

	starttime, err := time.Parse("2006-01-02 15:04:05", starttimeStr)
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "开始时间设置发生错误,请重试!")
		return
	}

	endtime, err := time.Parse("2006-01-02 15:04:05", endtimeStr)
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "结束时间设置发生错误,请重试!")
		return
	}

	levelStr := ctx.PostForm("level")
	level, err := strconv.Atoi(levelStr)
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "紧急程度设置发生错误，请重试！")
		return
	}

	task = &model.Task{
		Account:   account,
		Title:     title,
		StartTime: starttime,
		EndTime:   endtime,
		Level:     level,
	}
	return task
}
